//
//  PickerViewController.m
//  NekaPrice
//
//  Created by yu on 2018/6/13.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "PickerViewController.h"

@interface PickerViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong)NSArray *dataSource;
@property (nonatomic, assign)NSInteger currentRow;
@end

@implementation PickerViewController

- (instancetype)initWithDataSource:(NSArray *)dataSource {
    if (self = [super init]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
        _currentRow = 0;
        _dataSource = dataSource;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGFloat height = self.view.frame.size.height * 0.3;
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, K_HEIGHT - height, K_WIDTH, height)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, K_HEIGHT - height, K_WIDTH, height)];
    
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    _pickerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_pickerView];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, _pickerView.frame.origin.y - 44, K_WIDTH, 44)];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#F1EDF6"];
    [self.view addSubview:headerView];
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 44)];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:cancelBtn];
    
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(K_WIDTH - 60, 0, 60, 44)];
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor colorWithHexString:@"#436EEE"] forState:UIControlStateNormal];
    
    [confirmBtn addTarget:self action:@selector(confirm) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:confirmBtn];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [_pickerView selectRow:_currentRow inComponent:0 animated:YES];
}

- (void)cancel {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)confirm {
    if (_confirmBlock != nil) {
        NSDictionary *data = _dataSource[_currentRow];
        _confirmBlock(data);
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return _dataSource.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    if (_titleForRowBlock) {
        return _titleForRowBlock(_dataSource, row);
    }else {
        return _dataSource[row];
    }

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    _currentRow = row;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
